package com.system.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.model.Course;
import com.system.service.CourseService;
import com.system.util.ReflectUtil;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class studentServlet
 */
public class CourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		response.setHeader("content-type", "text/html; charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*"); 
		ReflectUtil.invokeMethods(this.getClass(), request, response);
	}
	/**
	 * 查询课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CourseService service=new CourseService();
		JSONObject json=service.queryAll();
		response.getWriter().write(json.toString());
	}
	/**
	 * 查询课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void queryInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String courseName = request.getParameter("courseName");
		CourseService service=new CourseService();
		JSONObject json=service.queryInfo(courseName);
		response.getWriter().write(json.toString());
	}
	/**
	 * 添加课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String s = request.getParameter("teacher_id");
		int teacher_id = Integer.parseInt(s);
		String coursename = request.getParameter("coursename");
		
		Course course = new Course();
		course.setTeacher_id(teacher_id);
		course.setCoursename(coursename);
		
		CourseService service = new CourseService();
		JSONObject json=service.add(course);
		response.getWriter().write(json.toString());
	}
	/**
	 * 修改课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] names = request.getParameterValues("coursenames");
		
		CourseService service=new CourseService();
		JSONObject json=service.deleteCourses(names);
		response.getWriter().write(json.toString());
	}
	/**
	 * 删除课程信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void modify(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String s = request.getParameter("teacher_id");
		int teacher_id = Integer.parseInt(s);
		String coursename=request.getParameter("courename");
		
		Course course = new Course();
		course.setCoursename(coursename);
		course.setTeacher_id(teacher_id);
		
		CourseService service=new CourseService();
		JSONObject json=service.modify(course);
		response.getWriter().write(json.toString());
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
